
libname NY_INP "D:\Data\Hospital Inpatient Records\Hospital New York (1992-2008)";
libname AHA "C:\Data\AHA Survey";

%let keep_v1 = AGE asource DX1 PAY1 DSHOSPID;



%macro pull_ny(lib,file1,file2,file3,keep_v2,keep_v3,keep_v4,keep_v5,keep_v6,keep_v7,ln);
proc freq data = &lib..&file1.;
   tables   &keep_v5.;
   title "&file1. gender";
   where age in (22,23);
run;
data junk1 ;
   format state $2.;
   set &lib..&file1. (keep = &keep_v1. &keep_v2. &keep_v3. &keep_v4. &keep_v5. &keep_v6. &keep_v7.);
   file = "&file1.";
   state = substr("&lib.",1,2);
   if substr("&file1.",14,4) in (1995,1996,1997) then do;
      months_23 = intck('month',mdy(month(dob),14,year(dob)+23),mdy(month(ADATE),15,year(ADATE)));
	  ad_date = mdy(month(ADATE),15,year(ADATE));;
   end; 
   if substr("&file1.",14,4) not in (1995,1996,1997) then do;
      months_23 = intck('month',mdy(bmonth,14,byear+23),mdy(amonth,15,ayear));
	  ad_date = mdy(amonth,15,ayear);
   end;
   if months_23 = . then bad_date = 1; else bad_date = 0;
   from_ED = 0;
   if asource = "1" then from_ED = 1; 
   if &keep_v7. = "7" then from_ED = 1;
   if &keep_v6. = 20 then died = 1; else died = 0;
   if substr(DX1,1,2) in ('63','64','65','66','67') then preg_related = 1; else preg_related = 0;
   if (months_23 >= -40 and months_23 <= 40) or bad_date = 1;
   male = 0;
   if substr("&file1.",14,4) <= 1997 and &keep_v5. = 1 then male = 1; 
   if substr("&file1.",14,4) > 1997 and &keep_v5. = 0 then male = 1; 
   if substr("&file1.",14,4) <= 1997 and missing(&keep_v5.) then male = .; 
   if substr("&file1.",14,4) > 1997 and missing(&keep_v5.) then male = .; 
   count = 1;
	if ad_date < mdy(1,1,1992) then delete;
run;
proc freq data = junk1;
   tables count;
   where  bad_date = 1 and age in (22,23);
   title "&file1. Missing Exact Age";
run;


proc sort data = junk1; by DSHOSPID; run;

proc sort data = &lib..&file2. (keep = DSHOSPID AHAID) out = junk2;  by AHAID; run;

data junk3;
   merge junk2 (in = ina) AHA.&file3.;
   by AHAID;
   if ina = 1;
run;

proc sort data = junk3;   by DSHOSPID; run;

data &file1. (drop = DSHOSPID &keep_v4.);
   merge junk1 (in = ina) junk3;
   by DSHOSPID; 
   if ina = 1;
	if bad_date = 1 then delete;
	visit = 1;
   if cntrl in (12,13,14,15,16) then cntrl_public = 1; else cntrl_public = 0;
   if cntrl in (21,23) then cntrl_non_profit = 1; else cntrl_non_profit = 0;
   if cntrl in (31,32,33) then cntrl_for_profit = 1; else cntrl_for_profit = 0;
   if cntrl not in (12,13,14,15,16,21,23,31,32,33) then cntrl_other = 1; else cntrl_other = 0;
   if pay1 = 2 then medicaid = 1; else medicaid = 0;
   if pay1 = 3 then private = 1; else private = 0;
   if pay1 = 4 then self_pay = 1; else self_pay = 0;
   if pay1 not in (2,3,4) then other_ins = 1; else other_ins = 0;
run;
proc datasets;
   delete junk1 junk2 junk3;
run;

%mend;
%pull_ny(NY_INP,Ny_inpatient_1992,ny_sidc_1992_ahal,aha_extract1992,DX1,DX2-DX9,bmonth byear amonth ayear,sex,DISP,ayear,9);
%pull_ny(NY_INP,Ny_inpatient_1993,ny_sidc_1993_ahal,aha_extract1993,DX1,DX2-DX11,bmonth byear amonth ayear,sex,DISP,ayear,11);
%pull_ny(NY_INP,Ny_inpatient_1994,ny_sidc_1994_ahal,aha_extract1994,DX1,DX2-DX17,bmonth byear amonth ayear,sex,DISP,ayear,17);
%pull_ny(NY_INP,Ny_inpatient_1995,ny_sidc_1995_ahal,aha_extract1995,DX1,DX2-DX17,DOB ADATE,sex,DISP,DOB,17); 
%pull_ny(NY_INP,Ny_inpatient_1996,ny_sidc_1996_ahal,aha_extract1996,DX1,DX2-DX17,DOB ADATE,sex,DISP,DOB,17);
%pull_ny(NY_INP,Ny_inpatient_1997,ny_sidc_1997_ahal,aha_extract1997,DX1,DX2-DX17,DOB ADATE,sex,DISP,DOB,17);
%pull_ny(NY_INP,Ny_inpatient_1998,ny_sidc_1998_ahal,aha_extract1998,DX1,DX2-DX17,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17);
%pull_ny(NY_INP,Ny_inpatient_1999,ny_sidc_1999_ahal,aha_extract1999,DX1,DX2-DX17,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17);
%pull_ny(NY_INP,Ny_inpatient_2000,ny_sidc_2000_ahal,aha_extract2000,DX1,DX2-DX17,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17);
%pull_ny(NY_INP,Ny_inpatient_2001,ny_sidc_2001_ahal,aha_extract2001,DX1,DX2-DX17,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17);  
%pull_ny(NY_INP,Ny_inpatient_2002,ny_sidc_2002_ahal,aha_extract2002,DX1,DX2-DX17,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17); 
%pull_ny(NY_INP,Ny_inpatient_2003,ny_sidc_2003_ahal,aha_extract2003,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17); 
%pull_ny(NY_INP,Ny_inpatient_2004,ny_sidc_2004_ahal,aha_extract2004,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17); 
%pull_ny(NY_INP,Ny_inpatient_2005,ny_sidc_2005_ahal,aha_extract2005,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17); 
%pull_ny(NY_INP,Ny_inpatient_2006,ny_sidc_2006_ahal,aha_extract2006,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,ayear,17); *Use year as a palce holder here;
%pull_ny(NY_INP,Ny_inpatient_2007,ny_sidc_2007_ahal,aha_extract2007,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,PointOfOriginUB04,17); 
%pull_ny(NY_INP,Ny_inpatient_2008,ny_sidc_2008_ahal,aha_extract2008,ECODE1,DX2-DX15,bmonth byear amonth ayear,FEMALE,DISPUNIFORM,PointOfOriginUB04,17); 

data New_york_inp; 
   format ad_date date.;
   set Ny_inpatient_1992 Ny_inpatient_1993 Ny_inpatient_1994 Ny_inpatient_1995 Ny_inpatient_1996 Ny_inpatient_1997 Ny_inpatient_1998 Ny_inpatient_1999 Ny_inpatient_2000 
       Ny_inpatient_2001 Ny_inpatient_2002 Ny_inpatient_2003 Ny_inpatient_2004 Ny_inpatient_2005 Ny_inpatient_2006 Ny_inpatient_2007 Ny_inpatient_2008;
run;

%macro stab(i_file,o_file);
proc means data = &i_file. noprint;
   var visit male female preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   output out = junk1 sum = visit male female preg_related from_ED medicaid private self_pay other_ins cntrl_public cntrl_non_profit cntrl_for_profit cntrl_other;
   class ad_date;
   where months_23 >= -12 and months_23 <=12;
run;
data junk2;
   format ad_date date.;
   set junk1;
   if _type_ = 0 then delete;
run; 
PROC EXPORT DATA= junk2
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\&o_File..csv"
            DBMS=CSV REPLACE;
RUN;
proc datasets;
   delete junk1 junk2;
run;
%mend;

%stab(New_york_inp,P06 New York Inpatient);

%macro pull_v(suf,a_where);
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk1 (rename=(_FREQ_ = &st._&suf.)) mean =  &st._medi_&suf. &st._priv_&suf. &st._self_&suf. &st._o_ins_&suf.;
   class months_23;
   where state = "&st." &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk2 (rename=(_FREQ_ = &st._&suf._ED)) mean =  &st._medi_&suf._ED &st._priv_&suf._ED &st._self_&suf._ED &st._o_ins_&suf._ED;
   class months_23;
   where state = "&st." and from_ED = 1 &a_where.;
run;
proc means data = &i_file. noprint;
   var   medicaid private self_pay other_ins;
   output out = junk3 (rename=(_FREQ_ = &st._&suf._NED)) mean =  &st._medi_&suf._NED &st._priv_&suf._NED &st._self_&suf._NED &st._o_ins_&suf._NED;
   class months_23;
   where state = "&st." and from_ED = 0 &a_where.;
run;
data &suf.;
   merge junk1 junk2 junk3;
   by months_23;
run;
%mend;

%macro p_state(st,i_File);
   %pull_v(all,);
   %pull_v(all_pub,and cntrl_public = 1);
   %pull_v(all_non_prof,and cntrl_non_profit = 1);
   %pull_v(all_for_prof,and cntrl_for_profit = 1);
   %pull_v(all_other,and cntrl_other = 1);
   %pull_v(all_np,and preg_related = 0);
   %pull_v(all_np_pub,and preg_related = 0 and cntrl_public = 1);
   %pull_v(all_np_non_prof,and preg_related = 0 and cntrl_non_profit = 1);
   %pull_v(all_np_for_prof,and preg_related = 0 and cntrl_for_profit = 1);
   %pull_v(all_np_other,and preg_related = 0 and cntrl_other = 1);
   %pull_v(female,and male = 0);
   %pull_v(female_pub,and male = 0 and cntrl_public = 1);
   %pull_v(female_non_prof,and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_for_prof,and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_other,and male = 0 and cntrl_other = 1);
   %pull_v(female_np,and preg_related = 0 and male = 0);
   %pull_v(female_np_pub,and preg_related = 0 and male = 0 and cntrl_public = 1);
   %pull_v(female_np_non_prof,and preg_related = 0 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_np_for_prof,and preg_related = 0 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_np_other,and preg_related = 0 and male = 0 and cntrl_other = 1);
   %pull_v(female_p,and preg_related = 1 and male = 0);
   %pull_v(female_p_pub,and preg_related = 1 and male = 0 and cntrl_public = 1);
   %pull_v(female_p_non_prof,and preg_related = 1 and male = 0 and cntrl_non_profit = 1);
   %pull_v(female_p_for_prof,and preg_related = 1 and male = 0 and cntrl_for_profit = 1);
   %pull_v(female_p_other,and preg_related = 1 and male = 0 and cntrl_other = 1);
   %pull_v(male,and male = 1);
   %pull_v(male_pub,and male = 1 and cntrl_public = 1);
   %pull_v(male_non_prof,and male = 1 and cntrl_non_profit = 1);
   %pull_v(male_for_prof,and male = 1 and cntrl_for_profit = 1);
   %pull_v(male_other,and male = 1 and cntrl_other = 1);
   data inter.ins_23_reprod_&ST. (drop = _type_ );
      merge all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
      by months_23;
      if months_23 = . then delete;
      merge_v = 1;
   run;
%mend;

%p_state(NY,New_york_inp);


proc datasets;
   delete all 		all_pub 		all_non_prof 		all_for_prof 		all_other
       	 	all_np 		all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         	female 		female_pub 		female_non_prof 	female_for_prof 	female_other
   		 	female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
   		 	female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
   		 	male 		male_pub 		male_non_prof 		male_for_prof 		male_other

       Ny_inpatient_1992 Ny_inpatient_1993 Ny_inpatient_1994 Ny_inpatient_1995 Ny_inpatient_1996 
       Ny_inpatient_1997 Ny_inpatient_1998 Ny_inpatient_1999 Ny_inpatient_2000 
       Ny_inpatient_2001 Ny_inpatient_2002 Ny_inpatient_2003 Ny_inpatient_2004 Ny_inpatient_2005 
       Ny_inpatient_2006 Ny_inpatient_2007 Ny_inpatient_2008;
run;


